

#read in the candidate broker conjoint experiment data
select_df <- read_csv("./data/candidate_broker_data.csv")


stack_1 <-
  select_df %>%
  select(V_53_E1, V_53_G1, V_53_R1, V_53_I1, V_53_S1, V_53_O1, V_53,
         V_1, V_5, V_6, B) %>%
  mutate_all(as.character) %>%
  mutate(V_6 = case_when(V_6 == "Lainnya" ~ NA_character_,
                         TRUE ~ V_6)) %>%
  mutate(eth_match = (V_6 == V_53_S1)*1) %>%
  mutate(gend_match = (V_1 == V_53_G1)*1) %>%
  mutate(relg_match = (V_53_R1 == V_5)*1) %>%
  mutate(selected = (V_53 == "Tokoh 1")*1)


stack_2 <-
  select_df %>%
  select(V_53_E1 = V_53_E2, V_53_G1 = V_53_G2, V_53_R1 = V_53_R2, V_53_I1 = V_53_I2, V_53_S1 = V_53_S2, V_53_O1 = V_53_O2, V_53,
         V_1, V_5, V_6, B) %>%
  mutate_all(as.character) %>%
  mutate(V_6 = case_when(V_6 == "Lainnya" ~ NA_character_,
                         TRUE ~ V_6)) %>%
  mutate(eth_match = (V_6 == V_53_S1)*1) %>%
  mutate(gend_match = (V_1 == V_53_G1)*1) %>%
  mutate(relg_match = (V_53_R1 == V_5)*1) %>%
  mutate(selected = (V_53 == "Tokoh 2")*1)

stack <- bind_rows(stack_1, stack_2)

cand_plot <-
  lm_robust(selected ~ V_53_E1 + V_53_G1 + relg_match + V_53_I1 + eth_match + V_53_O1, data = stack, clusters = B) %>%
  tidy_and_attach() %>%
  select(term, estimate, std.error) %>%
  tidy_add_reference_rows() %>%
  tidy_add_estimate_to_reference_rows() %>% 
  filter(term != "(Intercept)") %>%
  mutate(variable_nice = case_when(variable == "V_53_E1" ~ "Education",
                                   variable == "V_53_G1" ~ "Gender",
                                   variable == "V_53_I1" ~ "Income",
                                   variable == "V_53_O1" ~ "Occupation",
                                   variable == "relg_match" ~ "Matched: Religion",
                                   variable == "eth_match" ~ "Matched: Ethnicity"
  )) %>%
  mutate(term_nice = case_when(term == "V_53_E1Sekolah menengah" ~ "High school",
                               term == "V_53_E1Universitas" ~ "University",
                               term == "V_53_G1Laki-laki" ~ "Man",
                               term == "V_53_G1Perempuan" ~ "Woman",
                               term == "relg_match" ~ "Religious Match == Yes",
                               term == "V_53_I1<3jt" ~ "< 3m IDR",
                               term == "V_53_I1>10jt" ~ "> 10m IDR",
                               term == "eth_match" ~ "Ethnicity Match == Yes",
                               term == "V_53_O1Petani" ~ "Farmer",
                               term == "V_53_O1PNS" ~ "Civil servant",
                               term == "V_53_O1Ustad/Imam" ~ "Ustad/Imam",
                               term == "V_53_O1Wiraswasta" ~ "Businessman")) %>%
  mutate(across(c(term_nice, variable_nice), ~fct_inorder(.))) %>%
  ggplot(aes(y=term_nice, x = estimate)) +
  geom_point() +
  geom_errorbarh(aes(xmin = estimate - 1.96*std.error, xmax = estimate + 1.96*std.error, height = 0)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  facet_col(facets = "variable_nice", scales = "free_y", space = "free") +
  theme_bw()+ 
  theme(
    axis.line.x.bottom = element_line(color = "black"),
    axis.line.y.left = element_blank(),
    text = element_text(size=14),
    panel.grid.minor = element_blank(),
    legend.position = "none",
    axis.title.y = element_blank(),
    panel.border = element_blank(),
    axis.ticks.x = element_blank()) +
  xlab("Percentage point change in probability of selection")

ggsave(filename = "./outputs/figures/figure_a1.pdf", plot = cand_plot, width = 7, height = 5)





